var AjaxDataSource = function(options) {
	this._formatter = options.formatter;
	this._columns = options.columns;
	this._url = options.url;
	// options.param {"name":"张三"，"age":20}
	var string = options.param || {};
	this._param = string;
};

AjaxDataSource.prototype = {
	columns : function() {
		return this._columns;
	},

	setUrl : function(url, param) {
		this._url = url || this._url;
		var string = param|| {};
		this._param = string;
	},

	data : function(options, callback) {
		var url = this._url;
		var param = this._param;
		var self = this;
		if (true) {
			param.perpage = options.pageSize;
			param.page = options.pageIndex + 1;
			// 服务器端排序
			if (options.sortProperty) {
				param.sort = options.sortProperty;
				param.sortdir = options.sortDirection;
			}
			var paramString = [];
			$.each(param, function(k, v) {
				paramString.push("&");
				paramString.push(k);
				paramString.push("=");
				paramString.push(v);
			});

			$.ajax(url, {
				dataType : 'json',
				type : 'POST',
				data : paramString.join('')

			}).done(function(response) {
				var data = response.data;
				var count = response.count;
				var startIndex = (response.page - 1) * response.perpage;
				var endIndex = startIndex + response.perpage;// 每一页条数
				var end = (endIndex > count) ? count : endIndex;
				var pages = response.pages;// 共几页
				var page = response.page;// 第几页
				var start = startIndex + 1;

				if (self._formatter)
					self._formatter(data);
				callback({
					data : data,
					start : start,
					end : end,
					count : count,
					pages : pages,
					page : page
				});

			});

		} else {
			callback({
				data : [],
				start : 0,
				end : 0,
				count : 0,
				pages : 0,
				page : 0
			});

		}

	}
};
